home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C25 / SingletonPattern2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  702 b   |  30 lines

  1. //: C25:SingletonPattern2.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. class Singleton {
  10.   int i;
  11.   Singleton(int x) : i(x) { }
  12.   void operator=(Singleton&);
  13.   Singleton(const Singleton&);
  14. public:
  15.   static Singleton& getHandle() {
  16.     static Singleton s(47);
  17.     return s;
  18.   }
  19.   int getValue() { return i; }
  20.   void setValue(int x) { i = x; }
  21. };
  22.  
  23. int main() {
  24.   Singleton& s = Singleton::getHandle();
  25.   cout << s.getValue() << endl;
  26.   Singleton& s2 = Singleton::getHandle();
  27.   s2.setValue(9);
  28.   cout << s.getValue() << endl;
  29. } ///:~
  30.